package com.ffyc.cms.controller;

import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.Admin;
import com.ffyc.cms.service.JournalismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 新闻控制层
 */
@RestController
@RequestMapping(path = "journalism")
public class JournalismController {
    @Autowired
    JournalismService  journalismService;

    /**
     * 根据状态查新闻
     * @param state
     * @return
     */
    @RequestMapping(path = "getjournalism")
    public CommonResult getjournalism(Integer state,Integer id){
        CommonResult commonResult;
        try {
            List<JournalismController> list = journalismService.getjournalism(state,id);
            commonResult = new CommonResult(200,"查找新闻成功",list);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙，请稍后重试",null);
            return commonResult;
        }
        return commonResult;
    }

    @RequestMapping(path = "getjournalismByWriter")
    public CommonResult getjournalismByWriter(HttpSession session){
        CommonResult commonResult;
        try {
            Admin admin = (Admin)session.getAttribute("admin");
            List<JournalismController> list = journalismService.getjournalismByWriter(admin);
            commonResult = new CommonResult(200,"查找新闻成功",list);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙，请稍后重试",null);
            return commonResult;
        }
        return commonResult;
    }

    /**
     * 根据状态查新闻
     * @param state
     * @return
     */
    @RequestMapping(path = "passcheck")
    public CommonResult passcheck(Integer id,Integer state){
        CommonResult commonResult;
        try {
            journalismService.passcheck(id,state);
            commonResult = new CommonResult(200,"返回",null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙，请稍后重试",null);
            return commonResult;
        }
        return commonResult;
    }
    /**
     * 查询每种新闻数量
     * @return
     */
    @RequestMapping(path = "getjournalismnum")
    public CommonResult getjournalismnum(){
        CommonResult commonResult;
        try {
            Map<String,List> map = journalismService.getjournalismnum();
            commonResult = new CommonResult(200,"查找成功",map);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙，请稍后重试",null);
            return commonResult;
        }
        return commonResult;
    }
}
